package com.sky.service.impl;


import ch.qos.logback.core.status.Status;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 套餐业务层
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetmealInfo(SetmealDTO setmealDTO) {
        //1、封装数据层实体类
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmeal.setStatus(Status.INFO);
        //2、保存套餐信息
        setmealMapper.addSetmealInfo(setmeal);
        //3、保存套餐和菜品关联关系
        setmealDishes.stream().forEach(f-> f.setSetmealId(setmeal.getId()));
        setmealDishMapper.addSetmealInfo(setmealDishes);
    }

    /**
     * 分页查询套餐信息
     *
     * @param setmealDTO
     * @return
     */
    @Override
    public PageResult setmealPageQuery(SetmealPageQueryDTO setmealDTO) {
        //1、包装分页函数
        PageHelper.startPage(setmealDTO.getPage(),setmealDTO.getPageSize()," s.create_time desc");
        //2、包装数据层实体类
        //3、查询数据层
        Page<SetmealVO> page =  setmealMapper.selectSetmealPageQuery(setmealDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByID(Long id) {
        return setmealMapper.getByID(id);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateSetmealInfo(SetmealDTO setmealDTO) {
        Long id = setmealDTO.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //1、包装数据层实体类
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //2、删除套餐关联菜品信息
        setmealDishMapper.deleteDishById(id);
        //3、新增套餐关联菜品信息
        if(!ObjectUtils.isEmpty(setmealDishes)){
            setmealDishes.stream().forEach(f-> f.setSetmealId(id));
            setmealDishMapper.addSetmealInfo(setmealDishes);
        }
        //4、修改套餐信息
        setmealMapper.updateSetmealInfo(setmeal);
    }

    /**
     * 根据id批量删除套餐
     *
     * @param ids
     */
    @Override
    public void deleteIds(List<Long> ids) {
        //1、判断套餐是否在售
        Integer num =  this.setmealMapper.selectMealIsNoStatus(ids);
        if(num > 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //2、删除套餐下关联的所有菜品
        for (Long id : ids) {
            setmealDishMapper.deleteDishById(id);
        }
        //3、删除所有套餐
        this.setmealMapper.deleteMealS(ids);
    }

    /**
     * 修改状态
     *
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        //如果是启售需要判断下面有无停售菜品
        if(status == 1){
            int num = setmealMapper.isSetmealDishStatus(id);
            if(num > 0){
                throw new DeletionNotAllowedException("套餐下有停售菜品，无法起售");
            }
        }
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.updateSetmealInfo(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
